//
//  ByCellIndexFuction.m
//  jinpuyiyuan_ios
//
//  Created by mc on 2017/12/5.
//  Copyright © 2017年 mc. All rights reserved.
//

#import "ByCellIndexFuction.h"

// vc
//#import "ByUserIndexRegistrationVC.h"       // 挂号
#import "ByUserIndexLookDoctorVC.h"         // 找医生
#import "ByServiceForInHospitalVC.h"        // 住院服务
#import "ByServiceForPhysicalCheckVC.h"     // 体检服务

@implementation ByCellIndexFuction

+ (instancetype)cellWithTableView:(UITableView*)tableView{
    
    static NSString *cellIdentifier = @"ByCellIndexFuction";
    ByCellIndexFuction *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    return cell;
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSeparatorStyleNone;
        [self createViews];
    }
    return self;
}

- (void)createViews{
    NSArray *imgsAry = @[@"Index_guahao",@"Index_zhaoyisheng",@"Index_zhuyuanfuwu",@"Index_tijianfuwu"];
    NSArray *textAry = @[@"挂号",@"找医生\n在线咨询",@"住院服务",@"体检服务"];
    CGFloat x, y,width,height;
    for (int i = 0; i < textAry.count; i ++) {
        width = SCREEN_WIDTH / 2;
        height = SCREEN_WIDTH / 4;
        x = (i % 2) * width ;
        y = (i / 2) * height ;
        UIControl *view = [self createHorizontalModeView:CGRectMake(x, y, width, height)
                                               imageSize:CGSizeMake(height * 0.5, height * 0.5)
                                               imageName:imgsAry[i]
                                                textFont:FONT(18)
                                                    text:textAry[i]
                                                  margin:7];
        view.layer.cornerRadius = 4;
        view.tag = 10 + i;
        [self.contentView addSubview:view];
        [view addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    // 横向线
    UIView *lineRow = [[UIView alloc] initWithFrame:CGRectMake(10, SCREEN_WIDTH / 4, SCREEN_WIDTH - 20, 1)];
    lineRow.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self addSubview:lineRow];
    
    // 纵向
    UIView *lineCol = [[UIView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, 0, 1, SCREEN_WIDTH / 2)];
    lineCol.backgroundColor = [UIColor colorWithWhite:0.90 alpha:1];
    [self addSubview:lineCol];

    
}

// 创建水平排版的视图（可点击）
- (UIControl *)createHorizontalModeView:(CGRect)frame
                              imageSize:(CGSize)size
                              imageName:(NSString *)name
                               textFont:(UIFont *)font
                                   text:(NSString *)text
                                 margin:(CGFloat)margin {
    
    // 1.创建基底视图
    UIControl *basalView = [[UIControl alloc] initWithFrame:frame];
    basalView.backgroundColor = [UIColor whiteColor];
    
    // 2.创建图片
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, size.width, size.height)];
    imageView.image = [UIImage imageNamed:name];
    [basalView addSubview:imageView];
    
    
    // 3.创建文字
    CGFloat fontSize = [font pointSize];
    CGFloat width = [text getSizeOfString:fontSize].width + 2; // 防止iOS10以上不适配
    CGFloat height = basalView.height;
    UILabel *textLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, width, height)];
    textLabel.numberOfLines = 0;
    textLabel.font = font;
    textLabel.textAlignment = NSTextAlignmentCenter;
    [basalView addSubview:textLabel];

    if ([text containsString:@"找医生"]) {
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:text];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor lightGrayColor] range:NSMakeRange(4, 4)];
        [str addAttribute:NSFontAttributeName value:FONT(13) range:NSMakeRange(4, 4)];
        textLabel.attributedText = str;

    }else{
        textLabel.text = text;
    }
    
    // 4.适配距离
    imageView.right = basalView.width / 2 - 20;
    imageView.y = basalView.height / 2;
    textLabel.left = imageView.right + margin;
    textLabel.y = basalView.height / 2;
    
    return basalView;
}

// 点击事件
- (void)tapAction:(UIControl *)control{
    // 挂号
    if (control.tag == 10) {
//        ByUserIndexRegistrationVC *vc = [[ByUserIndexRegistrationVC alloc] init];
//        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
        if (self.registraionBlock) {
            self.registraionBlock();
        }
    }
    // 找医生
    else if(control.tag == 11){
        ByUserIndexLookDoctorVC *vc = [[ByUserIndexLookDoctorVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 住院服务
    else if (control.tag == 12){
        ByServiceForInHospitalVC *vc = [[ByServiceForInHospitalVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
    // 体检服务
    else{
        ByServiceForPhysicalCheckVC *vc = [[ByServiceForPhysicalCheckVC alloc] init];
        [(ByBaseVC *)self.viewController by_normalPush:vc hasNavi:YES];
    }
}
@end
